package io.nutz.nutzsite.module.job.controller;

import io.nutz.nutzsite.common.base.Result;
import io.nutz.nutzsite.common.utils.ShiroUtils;
import io.nutz.nutzsite.module.job.models.Jobed;
import io.nutz.nutzsite.module.job.models.Jobpub;
import io.nutz.nutzsite.module.job.services.JobpubService;
import io.nutz.nutzsite.module.sys.models.User;
import io.nutz.nutzsite.module.sys.services.UserService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.plugins.slog.annotation.Slog;
/**
 * 作业任务 信息操作处理
 * 
 * @author lrw
 * @date 2020-03-06
 */
@IocBean
@At("/job/jobpub")
public class JobpubController {
	private static final Log log = Logs.get();

	@Inject
	private JobpubService jobpubService;
	@Inject
	private UserService userService;
	
	@RequiresPermissions("job:jobpub:view")
	@At("")
	@Ok("th:/job/jobpub/jobpub.html")
	public void index(HttpServletRequest req) {

	}

	/**
	 * 查询作业任务列表
	 */
	@RequiresPermissions("job:jobpub:list")
	@At
	@Ok("json")
	public Object list(@Param("pageNum")int pageNum,
					   @Param("pageSize")int pageSize,
					   @Param("name") String name,
					   @Param("beginTime") Date beginTime,
					   @Param("endTime") Date endTime,
					   @Param("orderByColumn") String orderByColumn,
					   @Param("isAsc") String isAsc,
					   HttpServletRequest req) {
		Cnd cnd = Cnd.NEW();
		if (!Strings.isBlank(name)){
			//cnd.and("name", "like", "%" + name +"%");
		}
		if(Lang.isNotEmpty(beginTime)){
			cnd.and("create_time",">=", beginTime);
		}
		if(Lang.isNotEmpty(endTime)){
			cnd.and("create_time","<=", endTime);
		}
		return jobpubService.tableList(pageNum,pageSize,cnd,orderByColumn,isAsc,null);
	}
	@At
	@Ok("json")
	public Object onePub(@Param("id") String id,HttpServletRequest req) {
		//NutMap map=NutMap.NEW();
		//map.addv("snpub", jobpubService.fetch(id));
		return jobpubService.fetch(id);//map;
	}
	/**
	 * 新增作业任务
	 */
	@At("/add")
	@Ok("th:/job/jobpub/add.html")
	public void add( HttpServletRequest req) {

	}

	/**
	 * 新增保存作业任务
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("job:jobpub:add")
	@Slog(tag="作业任务", after="新增保存作业任务 id=${args[0].id}")
	public Object addDo(@Param("..") Jobpub snpub,HttpServletRequest req) {
		try {
			List<Jobed> sneds=new ArrayList<Jobed>();
			if(snpub.isHasobj()){//指定签到人
				String[] objs=snpub.getSnobj().split(",");
				List<User> ulist=userService.query(Cnd.where("classg", "in", objs).or("id", "in", objs));
				int n=ulist.size();

				for(int i=0;i<n;i++){
					Jobed sned=new Jobed();
					sned.setUid(ulist.get(i).getId());
					sned.setStatus(-1);
					sned.setComment("未交");
					sned.setScore(0);
					sneds.add(sned);
				}
				snpub.setSneds(sneds);
				snpub.setCreateby(ShiroUtils.getSysUserId());
				snpub.setAbsentsn(n);//默认未签到人数
				jobpubService.insertWith(snpub,"sneds");
			}else{
				snpub.setCreateby(ShiroUtils.getSysUserId());
				jobpubService.insert(snpub);
			}

			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 修改作业任务
	 */
	@At("/edit/?")
	@Ok("th://job/jobpub/edit.html")
	public void edit(String id, HttpServletRequest req) {
		Jobpub jobpub = jobpubService.fetch(id);
		req.setAttribute("jobpub",jobpub);
	}

	/**
	 * 修改保存作业任务
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("job:jobpub:edit")
	@Slog(tag="作业任务", after="修改保存作业任务")
	public Object editDo(@Param("..") Jobpub jobpub,HttpServletRequest req) {
		try {
			if(Lang.isNotEmpty(jobpub)){

				jobpubService.update(jobpub);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 删除作业任务
	 */
	@At("/remove")
	@Ok("json")
	@RequiresPermissions("job:jobpub:remove")
	@Slog(tag ="作业任务", after= "删除作业任务:${array2str(args[0])}")
	public Object remove(@Param("ids")String[] ids, HttpServletRequest req) {
		try {
			jobpubService.delete(ids);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}
	/**
	 * 停止作业任务
	 */
	@At("/stop")
	@Ok("json")
	//@RequiresPermissions("signin:snpub:remove")
	@Slog(tag ="作业任务", after= "停止作业任务:${array2str(args[0])}")
	public Object stop(@Param("ids")String[] ids, HttpServletRequest req) {
		try {
			
			jobpubService.update(Chain.make("snendtime", new Date()).add("hasendtime", true), Cnd.where("id","=", ids[0]));
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}
}
